package com.eshop.modules.wechat.rest;

import org.springframework.beans.BeanUtils;
import com.eshop.modules.mp.service.dto.ArticleDto;
import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.mp.domain.Article;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.mp.service.dto.ArticleQueryCriteria;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import java.io.Serializable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import com.eshop.modules.services.WechatArticleService;
import com.eshop.modules.mp.service.ArticleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:微信图文管理" })
@RestController
@RequestMapping({ "api" })
public class WechatArticleController
{
    private final ArticleService articleService;
    private final WechatArticleService wechatArticleService;
    
    public WechatArticleController(final ArticleService articleService, final WechatArticleService wechatArticleService) {
        this.articleService = articleService;
        this.wechatArticleService = wechatArticleService;
    }
    
    @ApiOperation("查询单条信息")
    @GetMapping({ "/yxArticle/info/{id}" })
    @PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_GET')")
    public ResponseEntity getInfo(@PathVariable final Integer id) {
        return new ResponseEntity(this.articleService.getById((Serializable)id), HttpStatus.OK);
    }
    
    @ApiOperation("查询")
    @GetMapping({ "/yxArticle" })
    @PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_SELECT')")
    public ResponseEntity getYxArticles(final ArticleQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.articleService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ApiOperation("新增")
    @PostMapping({ "/yxArticle" })
    @PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody final Article resources) {
        return new ResponseEntity(this.articleService.save(resources), HttpStatus.CREATED);
    }
    
    @ApiOperation("修改")
    @PutMapping({ "/yxArticle" })
    @PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody final Article resources) {
        this.articleService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @ApiOperation("删除")
    @DeleteMapping({ "/yxArticle/{id}" })
    @PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer id) {
        this.articleService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
    
    @ForbidSubmit
    @ApiOperation("发布文章")
    @GetMapping({ "/yxArticle/publish/{id}" })
    @PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_DELETE')")
    public ResponseEntity publish(@PathVariable final Integer id) throws Exception {
        final ArticleDto articleDTO = new ArticleDto();
        final Article article = (Article)this.articleService.getById((Serializable)id);
        BeanUtils.copyProperties(article, articleDTO);
        this.wechatArticleService.publish(articleDTO);
        return new ResponseEntity(HttpStatus.OK);
    }
}
